var stats;
var currentTrooper = 0;
var right = $('td.right');

function Start(){
    if($(':password').length==0){
        $('td.left div.clear').append('<div class="but b2"><div class="but_right b2_right"><a id="troopers" onmouseout="_.resetTip(\'menuTip\')" onmouseover="_.showTip(\'menuTip\',\'Afficher les informations sur vos soldats\')" class="but_bg b2_bg" href="/t/add">Troopers</a></div></div>');
        $('#troopers').click(GetStats);
        UpdateActions();
    }
	
	self.port.on('BattleLoaded', function(html){
		console.debug('battle loaded !!');
		GetBattles(html);
	});
}

function UpdateActions(){    
    $('a.bgo2_bg').click(function(e){Battle($(this), e);});
    $('a.bgo_bg').click(function(e){Battle($(this), e);});
}

function GetStats(){
    Init();
    
    GetRecrute();
    
    return false;
}
        
function Init(){
    $('#statsbox').remove();
    right.prepend('<div id="statsbox" class="box2"><div class="box2bg"><h1 class="hbox">Troopers</h1></div><ul id="stats" style="padding:5px;"></ul><div class="box2bottom"><div class="hqtip"><img src="/img/info.png"> <a href="http://kewwwa.minitroopers.fr/">Créé par kewwwa</a></div></div></div></div>');
    stats = $('#stats');
}

function GetRecrute(){
    $.ajax({
        url: '/add',
        statusCode: {200:DisplayRecrute}
    });
}

function DisplayRecrute(data){
    var add = $(data).find('#valid').text();
    stats.append('<li class="clear"><div class="but b3"><div class="but_right b3_right"><a href="/add" class="but_bg b3_bg img">'+add+'<img src="/img/token.png"></a></div></div></li>');
    
    GetTroopers(0);
}

function GetTroopers(index){
    currentTrooper = index;
    $.ajax({
        url: '/t/'+index,
        statusCode: {200:DisplayTrooper}
    });    
}

function DisplayTrooper(data){
    var result = $(data);
    //var soldier = result.find('li.soldier');
    var skills = result.find('li.on');
    
    if(result.find('#config').length > 0){        
        stats.append('<li class="clear" style="margin:5px;"><a href="/t/'+currentTrooper+'">'+result.find('a.company').text()+'</a></li>');
        
        var $ul = $('<ul class="skills" style="width:auto;"/>');
        $ul.append(skills);
        
        var $div = $('<div class="sheet clear"/>');
        $div.append($ul);
        
        var $li = $('<li/>');
        $li.append($div);            
        stats.append($li);
        
        var $up = $('<li class="clear"/>');
        $up.append(result.find('td.sheetright div.but').first());
        stats.append($up);
        
        GetTroopers(++currentTrooper);
    }
}

function Battle(a, e){
    if(a.hasClass('bdefeat_bg'))
        return true;
    if(a.hasClass('bwin_bg'))
        return true;        
                
    CancelEvent(e);
    
    var batt = a.attr('href');
    if(batt=='/b/opp')
		//return self.port.emit('LoadBattle', 'http://'+window.location.host+'/b/opp');
        return GetBattles();
        
    $.get(batt, UpdateBattle);
    return false;
}
/*
function GetBattles(html){	
    $('#choose').remove();
	right.prepend('<table id="choose" class="choose"></table>');
	$('#choose').html(html);
	
	$('a.button5').click(function(e){
		CancelEvent(e);
		var batt = $(this).attr('href');
		$.get(batt, UpdateBattle);
    });
	
    return true;
}
*/
function GetBattles(){	
    $.get('/b/opp', function(data) {
        var result = $(data);        
        $('#choose').remove();
		right.prepend('<table id="choose" class="choose"></table>');
        $('#choose').html(result.find('table.choose').html());
		$('a.button5').each(function () {
			$(this).removeAttr('onclick')
			$(this).click(function(e){
				CancelEvent(e);
				var batt = $(this).attr('href');
				$.get(batt, UpdateBattle);
			});
		});
    });
    return true;
}

function UpdateBattle(){
    $.get('/hq', function(data) {
        var result = $(data);        
        right.empty();
        right.append(result.find('td.right').html());
        $('div.money').text(result.find('div.money').text());
        $('div.power').text(result.find('div.power').text());
        UpdateActions();
    });
}

function CancelEvent(e){
    e.stopPropagation();
    e.preventDefault();
}

Start();
